package com.example.javamaday.leetcode;

import java.util.HashMap;

public class LeetCodeTest0169 {

    public static void main(String[] args) {

    }

    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> numbers = new HashMap<>();

        int maxFind = nums[0];
        int maxFindNum = 0;

        for (int i = 0; i < nums.length; i++) {
            if (numbers.get(nums[i]) == null) {
                numbers.put(nums[i], 1);
                if (maxFindNum < 1) {
                    maxFind = nums[i];
                    maxFindNum = 1;
                }
            } else {
                numbers.put(nums[i], numbers.get(nums[i]) + 1);
                if (numbers.get(nums[i]) > maxFindNum) {
                    maxFind = nums[i];
                    maxFindNum = numbers.get(nums[i]);
                }
            }
        }
        return maxFind;
    }
}
